/* Framework for calling AJAX requests */
Ajax = {
	getUniqueIdentifier:function(){
		var id = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
		    return v.toString(16);
		});
		return id;
	},
	response:"",
	onfail:{},
	run:function(args){
		//function(method,onsuccess,args,dataType,onfail){
		if(typeof args == 'string'){console.log(arguments);alert('ajax: "' + args + '" needs conversion');}
		var myArgs = args;
		var method = myArgs.method;
		var dataType = myArgs.dataType || "";
		var onsuccess = myArgs.onsuccess;
		var onfail = myArgs.onfail;
		var args = myArgs.args;
		
		var id = Ajax.getUniqueIdentifier();
		var methodSplit = method.split('.');
		var testMode = false;
		
		if(typeof onfail == 'function'){
			Ajax.onfail[id] = onfail;
		}
		
		// save id to Ajax.success[id] so we can tell if success or no
		if(methodSplit.length == 3){ // look for third item to be modify behavior.  e.g. userService.login.test
			if(methodSplit[2].toLowerCase() == 'test'){
				testMode = true;
			}
		}
		var url = Ajax.url(methodSplit[0],methodSplit[1]);
		if(testMode){
			console.log(methodSplit);
			console.log(method + ' called.');
			console.log(args);
		}
		
		var ajax = {};
		ajax.url = url;
		ajax.type = "POST";
		ajax.data = args;
		if(dataType != ""){
			ajax.dataType = dataType;
		}
		ajax.success = function(data){
			if(!Ajax.isErrorResponse(data)){
				if(testMode){
					console.log(data);
				}
				if(typeof onsuccess == 'function'){
					onsuccess(data);
				}
				// update sessiontimeout
				if(methodSplit[0].toLowerCase().indexOf('view') > 0){ // injection
					$(document).trigger("document:changed");
				}
			} else {
				alert(data.Message+"\n\n"+data.TagContext[0].raw_trace);
				if(testMode){
					console.log(data);
				}
				$(document).trigger("document:changed");
				try{Ajax.onfail[id](arguments);}catch(e){/*do nothing*/}
			}
		};
		ajax.error = function(e,err){
			if(e.statusText != 'abort'){
				if(document.URL.indexOf('localhost') >= 0){
					//alert("Ajax error.");			
				}
				console.warn(e);
				try{Ajax.onfail[id](myArgs);}catch(e){/*do nothing*/}
			}
		};

		var result = $.ajax(ajax);
		
		return result;
	},
	isErrorResponse:function(variable){
		var valid = false;
		if(variable != null && typeof variable === 'object'){
			if('ErrorCode' in variable){
				valid = true;
			}
		}
		return valid;
	},
	url:function(cfc,method){
		return '/cfcs/remote.cfc?' + "cfc=" + cfc + "&callMethod=" + method + "&method=run";
	},
	getData:function($area){
		var data = {};
		$area.find('input,select').not('[type=checkbox],[type=radio]').each(function(){
			if($(this).closest(".hidden_forms").length == 0){
				data[this.name] = $(this).val() || "";
			}
		});
		$area.find('input[type=checkbox]').each(function(){
			if($(this).is(':checked') && $(this).closest(".hidden_forms").length == 0){
				data[this.name] = $(this).val() || "";
			}
		});
		$area.find('input[type=radio]').each(function(){
			var $this = $(this);
			if($this.closest(".hidden_forms").length == 0){
				if(!(this.name in data)){
					data[this.name] = "";
				}
				if($this.is(':checked')){
					data[this.name] = $this.val();
				}
			}
		});
		$area.find('textarea').each(function(){
			if($(this).closest(".hidden_forms").length == 0){
				data[this.name] = $(this).val() || "";
			}
		});
		return data;
	}
};